package com.mune.gulimall.seckill.config.interceptor;

import com.mune.common.constant.AuthServerConstant;
import com.mune.common.to.MemberSessionTo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

  public static ThreadLocal<MemberSessionTo> orderThread = new ThreadLocal<>();

  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

    // 如果当前请求uri 是/kill请求、则登录拦截
    String uri = request.getRequestURI();
    AntPathMatcher antPathMatcher = new AntPathMatcher();
    boolean match = antPathMatcher.match("/kill", uri);

    if (match) {
      // 从redis中获取用户session、
      MemberSessionTo memberSession = (MemberSessionTo) request.getSession().getAttribute(AuthServerConstant.LOGIN_USER);
      if (memberSession != null) {
        orderThread.set(memberSession);
        return true;
      } else {
        String contextPath = request.getContextPath();
        String curPath = request.getScheme() + "://" + request.getServerName() + ":" +
                request.getServerPort() + contextPath + request.getRequestURI();
        response.sendRedirect("http://auth.gulimall.com/login?redirect_url=" + curPath);
        return false;
      }
    }
    return true;
  }
}
